package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.PMAccountMgtExcution;
import com.fortune.dto.in.pMAccountMgt.PMAccountAddDto;
import com.fortune.dto.in.pMAccountMgt.PMAccountEditDto;
import com.fortune.dto.in.pMAccountMgt.PMAccountListDto;
import com.fortune.dto.in.pMAccountMgt.PMUserUpAndDownDto;
import com.fortune.enums.PMAccountMgtEnum;
import com.fortune.service.FindFhoneIsExistService;
import com.fortune.service.PMAccountMgtService;
import com.fortune.vo.pMAccountMgt.PMAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "pMAccountMgt", description = "平台厂商管理相关", tags = {"平台厂商管理相关"})
@RestController
@RequestMapping("pMAccountMgt")
public class PMAccountMgtController {
	@Autowired
	private PMAccountMgtService pmAccountMgtService;
	@Autowired
	private FindFhoneIsExistService findFhoneIsExistService;

	@ApiOperation(value = "厂商列表", nickname = "mAccountList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="string"),
			@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<PMAccount>> list(@ApiIgnore @Validated PMAccountListDto pmAccountListDto) {
		pmAccountListDto.setPage((pmAccountListDto.getPage()-1)*10);
		List<PMAccount> list = pmAccountMgtService.queryAccountList(pmAccountListDto);
		int count = pmAccountMgtService.queryAccountListCount(pmAccountListDto);
		PagingData<PMAccount> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new PMAccountMgtExcution<PagingData<PMAccount>>(pagingData, PMAccountMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "添加厂商", nickname = "mAccountAdd", notes = "m_user_info经销商员工账户员工编号默认为手机号", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="imgUrl", value="LogoURL",paramType="query",dataType="file",required = true),
			@ApiImplicitParam(name="name", value="厂商名称",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="number", value="厂商编号",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="abbreviation", value="厂商简称",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="principalPhone", value="负责人手机号",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="principalName", value="负责人姓名",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="dUpLimit", value="经销商数量上限",paramType="query",dataType="int",required = true),
			@ApiImplicitParam(name="endTime", value="截止时间(2016-07-06 10:17:48)",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="status", value="账户状态(YES,NO)",paramType="query",dataType="string",allowableValues = "YES,NO",required = true),
			@ApiImplicitParam(name="type", value="账户分类",paramType="query",dataType="string",required = true),
	})
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
	public BaseAPIExcution<Void> add(@ApiIgnore @Validated PMAccountAddDto pmAccountAddDto) {
//		int num = pmAccountMgtService.queryAccountIsExist(pmAccountAddDto);
//		if (num > 0){
//			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_EXIST);
//		}

		int num = pmAccountMgtService.queryAccountIsExistByName(pmAccountAddDto);
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_NAME_EXIST);
		}
		num = pmAccountMgtService.queryAccountIsExistByAbbreviation(pmAccountAddDto);
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_ABBRE_EXIST);
		}
		num = pmAccountMgtService.queryAccountIsExistByNumber(pmAccountAddDto);
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_NUMBER_EXIST);
		}

		//手机号
		num = findFhoneIsExistService.findFhoneIsExist(pmAccountAddDto.getPrincipalPhone());
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.ACCOUNT_PHONE_EXIST);
		}
		num = pmAccountMgtService.addAccount(pmAccountAddDto);
		if (num < 1){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.FAILURE);
		}
		return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "编辑厂商", nickname = "mAccountEdit", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="厂商ID",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="imgUrl", value="LogoURL",paramType="query",dataType="file"),
			@ApiImplicitParam(name="name", value="厂商名称",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="number", value="厂商编号",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="abbreviation", value="厂商简称",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="principalPhone", value="负责人手机号",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="principalName", value="负责人姓名",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="dUpLimit", value="经销商数量上限",paramType="query",dataType="int",required = true),
			@ApiImplicitParam(name="endTime", value="截止时间(2016-07-06 10:17:48)",paramType="query",dataType="string",required = true),
			@ApiImplicitParam(name="status", value="账户状态(YES,NO)",paramType="query",dataType="string",allowableValues = "YES,NO",required = true),
			@ApiImplicitParam(name="type", value="账户分类",paramType="query",dataType="string",required = true),
	})
	@PostMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
	public BaseAPIExcution<Void> edit(@ApiIgnore @Validated  PMAccountEditDto pmAccountEditDto) {
		int num = pmAccountMgtService.queryAccountById(pmAccountEditDto.getId());
		if (num < 1){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_NO_EXIST);
		}
//		num = pmAccountMgtService.queryEditAccountIsExist(pmAccountEditDto);
//		if (num > 0){
//			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_EXIST);
//		}

		num = pmAccountMgtService.queryEditAccountIsExistByName(pmAccountEditDto);
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_NAME_EXIST);
		}
		num = pmAccountMgtService.queryEditAccountIsExistByAbbreviation(pmAccountEditDto);
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_ABBRE_EXIST);
		}
		num = pmAccountMgtService.queryEditAccountIsExistByNumber(pmAccountEditDto);
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_NUMBER_EXIST);
		}


		//手机号检测
		num = findFhoneIsExistService.findAddMFhoneIsExist(pmAccountEditDto.getPrincipalPhone(),pmAccountEditDto.getId());
		if (num > 0){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.ACCOUNT_PHONE_EXIST);
		}
		num = pmAccountMgtService.editAccountCompanyId(pmAccountEditDto);
		if (num < 1){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.FAILURE);
		}
		return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.SUCCESS);
	}


	@ApiOperation(value = "删除厂商", nickname = "mAccountDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="acccountId", value="厂商ID",paramType="query",dataType="string")
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotEmpty(message="厂商ID不能为空") String acccountId) {
		int num = pmAccountMgtService.queryAccountById(acccountId);
		if (num < 1){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.ACCOUNT_NO_EXIST);
		}
		num = pmAccountMgtService.deleteAccount(acccountId);
		if (num < 1){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.FAILURE);
		}
		return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "重置厂商密码", nickname = "mAccountRsetPassword", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="acccountId", value="厂商ID",paramType="query",dataType="string")
	@PostMapping(value = "resetPassword",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(@Validated @NotEmpty(message="厂商ID不能为空") String acccountId) {
		String password = Constants.D_USER_PASSWORD;
		int num = pmAccountMgtService.resetPassword(acccountId,password);
		if (num < 1){
			return new PMAccountMgtExcution<Void>(null,PMAccountMgtEnum.FAILURE);
		}
		return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "账户状态", nickname = "mUserUpAndDown", notes = "@石匡")
	@PutMapping(value = "mUserUpAndDown",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> upAndDown(HttpServletRequest request,@Validated @RequestBody PMUserUpAndDownDto userUpAndDownDto){
		PMAccount distributorInfo = pmAccountMgtService.queryAccountInfoById(userUpAndDownDto.getId());
		if (distributorInfo == null){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_NO_EXIST);
		}
		if (String.valueOf(userUpAndDownDto.getType()).equals("YES") && distributorInfo.getStatus().equals(userUpAndDownDto.getType())){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_ENABLE);
		}else if(String.valueOf(userUpAndDownDto.getType()).equals("NO") && distributorInfo.getStatus().equals(userUpAndDownDto.getType())){
			return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.ACCOUNT_DISABLE);
		}else {
			int num = pmAccountMgtService.upAndDownUser(userUpAndDownDto);
			if (num < 1){
				return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.FAILURE);
			}
		}
		return new PMAccountMgtExcution<Void>(null, PMAccountMgtEnum.SUCCESS);
	}


}
